package com.gem.controller;

import com.gem.domain.StudentAPI;
import com.gem.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 学生接口API
 * create 2019/11/6 19:27
 * Modified By:
 */
@Controller
@RequestMapping("/factory")
public class StudentAPIController {

    //自动注入
    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Boolean> addStudent(@RequestBody List<StudentAPI> stuList, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Methods", "application/json;charset=UTF-8");
        Map<String, Boolean> map = new HashMap<>();
        try {
            System.err.println("连接成功！");
            System.err.println("stuList:" + stuList);
            if (studentService.addStuService(stuList)) {
                map.put("0", true);
            } else {
                map.put("0", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("0", false);
        }
        return map;
    }

    @RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Boolean> updateStudent(@RequestBody List<StudentAPI> stuList, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Methods", "application/json;charset=UTF-8");
        Map<String, Boolean> map = new HashMap<>();
        try {
            System.err.println("连接成功！");
            System.err.println("stuList:" + stuList);
            if (studentService.updateStuService(stuList.get(0))) {
                map.put("0", true);
            } else {
                map.put("0", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("0", false);
        }
        return map;
    }

    @RequestMapping(value = "/deleteStudent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Boolean> deleteStudent(@RequestBody List<StudentAPI> stuList, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Methods", "application/json;charset=UTF-8");
        Map<String, Boolean> map = new HashMap<>();
        System.err.println("连接成功！");
        System.err.println("stuList:" + stuList);
        try {
            if (studentService.deleteStuService(stuList.get(0).getId())) {
                map.put("0", true);
            } else {
                map.put("0", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("0", false);
        }
        return map;
    }
}
